package com.xin.springclouduser.service.impl;

import com.xin.springclouduser.dataobject.MyFriend;
import com.xin.springclouduser.dto.UserDTO;
import com.xin.springclouduser.repository.MyFriendRepository;
import com.xin.springclouduser.service.MyFriendService;
import com.xin.springclouduser.service.UserService;
import com.xin.springclouduser.utils.KeyUtil;
import com.xin.springclouduser.vo.ResultPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MyFriendServiceImpl implements MyFriendService {
    @Autowired
    private MyFriendRepository myFriendRepository;
    @Autowired
    private UserService userService;
    @Transactional
    public boolean create(String uid, String fid) {
        boolean ship = findFriendShip(uid, fid);
        if (ship){
             return false;
        }
        MyFriend myFriend=new MyFriend();
        myFriend.setId(KeyUtil.genUniqueKey());
        myFriend.setUid(uid);
        myFriend.setFid(fid);
        MyFriend friend = myFriendRepository.save(myFriend);
        if (ObjectUtils.isEmpty(friend)){
              return false;
        }
        return true;
    }

    @Transactional
    public boolean delete(String uid, String fid) {
        MyFriend myFriend = myFriendRepository.findByUidAndFid(uid, fid);
        if (ObjectUtils.isEmpty(myFriend)){
             myFriend=myFriendRepository.findByUidAndFid(fid,uid);
        }
        if (ObjectUtils.isEmpty(myFriend)){
              return false;
        }
        myFriendRepository.delete(myFriend);
        return true;
    }

    @Override
    public boolean findFriendShip(String uid, String fid) {
        MyFriend myFriend = myFriendRepository.findByUidAndFid(uid, fid);
        if (ObjectUtils.isEmpty(myFriend)){
              myFriend=myFriendRepository.findByUidAndFid(fid,uid);
        }
        if (ObjectUtils.isEmpty(myFriend)){
              return false;
        }
        return true;
    }

    @Override
    public ResultPage<UserDTO> findMyFriendList(String uid, Pageable pageable) {
        Page<MyFriend> page = myFriendRepository.findAllByUidOrFid(uid, uid, pageable);
        ResultPage<UserDTO> resultPage=new ResultPage<>();
        if (ObjectUtils.isEmpty(page)){
              return resultPage;
        }
        List<MyFriend> myFriends = page.getContent();
        List<UserDTO> userDTOS=new ArrayList<>();
        for (MyFriend friend: myFriends){
            // uid为本人id,fid为朋友ID
              if (friend.getUid().equals(uid)){
                  UserDTO userDTO = userService.findOne(friend.getFid());
                  userDTOS.add(userDTO);
              }
              if (friend.getFid().equals(uid)){
                  //uid为朋友ID,fid为本人ID
                  UserDTO userDTO = userService.findOne(friend.getUid());
                  userDTOS.add(userDTO);
              }
        }
        resultPage.setData(userDTOS);
        resultPage.setTotal(page.getTotalPages());
        resultPage.setPage(page.getNumber());
        resultPage.setSize(page.getSize());
        return resultPage;
    }
}
